﻿Maiks = LibStub("AceAddon-3.0"):NewAddon("Maiks", "AceEvent-3.0", "AceConsole-3.0")

RBGPopup = true

function Maiks:OnInitialize()
   
    
    -- ace3
    self.db = LibStub("AceDB-3.0"):New("Maiks_db", defaults, "Default")
    LibStub("AceConfig-3.0"):RegisterOptionsTable("Maiks", Maiks:createoptions())
    self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Maiks", "Maiks (core)")
    Maiks:RegisterChatCommand("MAIKS", "SlashCommands")
    Maiks:RegisterEvent("UPDATE_BATTLEFIELD_STATUS")
	-- Load default values
	Maiks:Defaults()
	Maiks:LoadAddons()
end
function Maiks:Defaults()
	if Maiks_db == nil then Maiks_GRdb = {} end
	if Maiks_db.loadatstart == nil then Maiks_db.loadatstart = {} end
	if Maiks_db.loadatstart.ar == nil then Maiks_db.loadatstart.ar = false end
	if Maiks_db.loadatstart.goodies == nil then Maiks_db.loadatstart.goodies = false end
	if Maiks_db.loadatstart.gr == nil then Maiks_db.loadatstart.gr = false end
	if Maiks_db.loadatstart.gt == nil then Maiks_db.loadatstart.gt = false end
	if Maiks_db.loadatstart.rbg == nil then Maiks_db.loadatstart.rbg = false end
end

function Maiks:LoadAddons()
	if Maiks_db.loadatstart.ar then Maiks:LoadAR() end
	if Maiks_db.loadatstart.goodies then Maiks:LoadGoodies() end
	if Maiks_db.loadatstart.gr then Maiks:LoadGR() end
	if Maiks_db.loadatstart.gt then Maiks:LoadGT() end
	if Maiks_db.loadatstart.rbg then Maiks:LoadRBG() end
end

function Maiks:UPDATE_BATTLEFIELD_STATUS()
    _,_,_,_, loadable,_,_ = GetAddOnInfo("Maiks-RBG")
    if loadable == 1 and RBGPopup then 
        if (not IsAddOnLoaded("Maiks-RBG")) then
            StaticPopup_Show ("RBGPOPUP")
        end
    end
end

StaticPopupDialogs["RBGPOPUP"] = {
    text = "Would you like to load Maiks: RBG?",
    button1 = "Yes",
    button2 = "No",
    OnAccept = function()
        Maiks:LoadRBG()
    end,
    OnCancel = function ()
        RBGPopup = false
    end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = true,
}

function Maiks:createoptions()

	options = {
		name = "Maiks",
		handler = Maiks,
		type = "group",
		args = {
			optionsgroup= {
				name = "Options",
				type = "group",
				order = 1,
		        args = {
					desc1 = {
						name = "Options for Maiks(core)",
						type = "description",
						order = 1,
					},
				},
			},
			loadaddons= {
				name = "Load AddOns",
				type = "group",
				order = 2,
				args = {
					header1 = {
						name = "Load other Maiks AddOns",
						type = "header",
						order = 1,
					},
					ardesc = {
						name = "Maiks: AR",
						type = "description",
						order = 1.1,
					},
					arloadatstart = {
                        name = "Load at start",
                        type = "toggle",
						disabled = function() if Maiks:AddonScan("Maiks-AR", "installed") == nil then return false else return true end end,
                        order = 1.2,
                        get = function() return Maiks_db.loadatstart.ar end,
                        set = function(info, val) Maiks_db.loadatstart.ar = val end,
                    },
					ar = {
                        name = "Load Now",
                        type = "execute",
						disabled = function() if Maiks:AddonScan("Maiks-AR", "installed") == nil then return false else return true end end,
                        order = 1.3,
                        func = function() Maiks:LoadAR() end,
                    },
					desc = {
						name = "Maiks: Goodies",
						type = "description",
						order = 2,
					},
					goodiesloadatstart = {
                        name = "Load at start",
                        type = "toggle",
						disabled = function() if Maiks:AddonScan("Maiks-Goodies", "installed") == nil then return false else return true end end, 
                        order = 2.1,
                        get = function() return Maiks_db.loadatstart.goodies end,
                        set = function(info, val) Maiks_db.loadatstart.goodies = val end,
                    },
					goodies = {
                        name = "Load Now",
                        type = "execute",
						disabled = function() if Maiks:AddonScan("Maiks-Goodies", "installed") == nil then return false else return true end end,
                        order = 2.2,
                        func = function() Maiks:LoadGoodies() end,
                    },
					desc2 = {
						name = "Maiks: GR",
						type = "description",
						order = 3,
					},
					grloadatstart = {
                        name = "Load at start",
                        type = "toggle",
						disabled = function() if Maiks:AddonScan("Maiks-GR", "installed") == nil then return false else return true end end,
                        order = 3.1,
                        get = function() return Maiks_db.loadatstart.gr end,
                        set = function(info, val) Maiks_db.loadatstart.gr = val end,
                    },
					gr = {
                        name = "Load Now",
                        type = "execute",
						disabled = function() if Maiks:AddonScan("Maiks-GR", "installed") == nil then return false else return true end end,
                        order = 3.2,
                        func = function() Maiks:LoadGR() end,
                    },
					desc3 = {
						name = "Maiks: GT",
						type = "description",
						order = 4,
					},
					gtloadatstart = {
                        name = "Load at start",
                        type = "toggle",
						disabled = function() if Maiks:AddonScan("Maiks-GT", "installed") == nil then return false else return true end end,
                        order = 4.1,
                        get = function() return Maiks_db.loadatstart.gt end,
                        set = function(info, val) Maiks_db.loadatstart.gt = val end,
                    },
					gt = {
                        name = "Load Now",
                        type = "execute",
						disabled = function() if Maiks:AddonScan("Maiks-GT", "installed") == nil then return false else return true end end,
                        order = 4.2,
                        func = function() Maiks:LoadGT() end,
                    },
					desc4 = {
						name = "Maiks: RBG",
						type = "description",
						order = 5,
					},
					rbgloadatstart = {
                        name = "Load at start",
                        type = "toggle",
						disabled = function() if Maiks:AddonScan("Maiks-RBG", "installed") == nil then return false else return true end end,
                        order = 5.1,
                        get = function() return Maiks_db.loadatstart.rbg end,
                        set = function(info, val) Maiks_db.loadatstart.rbg = val end,
                    },
					rbg = {
                        name = "Load Now",
                        type = "execute",
						disabled = function() if Maiks:AddonScan("Maiks-RBG", "installed") == nil then return false else return true end end,
                        order = 5.2,
                        func = function() Maiks:LoadRBG() end,
                    },
				},
			},      
		},
	}

return options
end